﻿using System;
using Common.Commands;
using Common.Commands.TrackLogCommands;
using Core.Domain.Model;
using Dto.ApiResponses.TrackLogResponses;
using Sanelib.DataOperation;

namespace Core.Processors.TrackLogProcessors
{
    public class UpdateTrackLogProcessor : ICommandProcessor<UpdateTrackLog>
    {
        private readonly IRepository<TrackLog> _trackLogsRepository;

        public UpdateTrackLogProcessor(IRepository<TrackLog> trackLogsRepository)
        {
            _trackLogsRepository = trackLogsRepository;
        }

        public dynamic Process(UpdateTrackLog command)
        {
            var trackLog = _trackLogsRepository.GetById(command.Id);

            if (trackLog == null)
            {
                throw new Exception("track log not found for tag: " + command.TagId);
            }

            trackLog.DetectedOn = command.DetectedOn;
            trackLog.ImageData = command.ImageData;
            trackLog.Description = command.Description;

            _trackLogsRepository.Update(trackLog);

            var trackLogResponse = new TrackLogResponse
            {
                Id = command.Id,
                Name = trackLog.Name,
                Code = trackLog.Code,
                RedirectRequired = false,
                DetectedOn = command.DetectedOn,
            };

            return trackLogResponse;
        }
    }
}